package com.wx.spider.common.util.randomport;

import org.springframework.util.StringUtils;

public class StartCommand {


    public StartCommand(int min, int max,String[] args) {
        Boolean isServerPort = false;
        String serverPort = "";
        String minPort = null;
        String maxPort = null;
        if (args != null) {
            //无效的方法
            for (String arg : args) {
                if (StringUtils.hasText(arg) && arg.startsWith("--server.port")) {
                    isServerPort = true;
                    serverPort = arg;
                    break;
                }
            }
            for (String arg : args) {
                if (StringUtils.hasText(arg) && arg.startsWith("--port.min")) {
                    minPort = arg;
                }
                if (StringUtils.hasText(arg) && arg.startsWith("--port.max")) {
                    maxPort = arg;
                }
                if (minPort != null && maxPort != null){
                    break;
                }
            }
        }
        // 没有指定端口, 则随机生成一个可用的端口
        if (!isServerPort) {
            int port = ServerPortUtils.getAvailablePort(min, max, null);
            System.setProperty("server.port", String.valueOf(port));
        } else {
            System.setProperty("server.port", serverPort.split("=")[1]);
        }
    }

}
